# -----------------------------------------------------------------------------
# Copyright (c) 2025, Southeast University (China)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#  - Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  - Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  - Neither the name of the copyright holders nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Jiajie Xu, Yanfang Zhang, Jiaqi Gao, Leyun Tian
# -----------------------------------------------------------------------------


import Global_var
import Interaction
import Timing_Propagation
import argparse

parser = argparse.ArgumentParser(description='This is the main python program for eco project.')

parser.add_argument('--design', type=str, required=True, default='s27', help='Set design name, default s27.')
parser.add_argument('--run_inn_pt', action='store_true', help='Execute innovus and primetime to generate initial data.')
parser.add_argument('--run_pt', action='store_true', help='Execute primetime only to generate initial data.')
parser.add_argument('--elmore', action='store_true', help='Use elmore delay model for net delay calculation.')
parser.add_argument('--clock_period', required=True, type=float, default=0.1, help='Set clock period for the design.')
parser.add_argument('--max_iteration', required=True, type=int, default=5, help='Set the max iteration for ECO.')
parser.add_argument('--contrast', action='store_true', help='Set lambda to 1, contrast study.')
parser.add_argument('--simple', action='store_true', help='A simple mode uses total capacitance for delay calculation, noted simple mode does not need elmore arg.')
args = parser.parse_args()


if __name__ == '__main__':
    print("Current arguments:")
    for arg, value in vars(args).items():
        print(f"  {arg}: {value}")
    if args.run_inn_pt:
        Interaction.Gen_Inn_output(args.design)
        Interaction.Gen_PT_output(args.design)
    elif args.run_pt:
        Interaction.Gen_PT_output(args.design)
    
    TP = Timing_Propagation.Timing_Propagation(design=args.design, contrast=args.contrast, clock_period=args.clock_period)
    if args.elmore:
        TP.ECO_Iteration(max_iteration=args.max_iteration, arnoldi=False, simple=args.simple)
    else:
        TP.ECO_Iteration(max_iteration=args.max_iteration, arnoldi=True, simple=args.simple)